#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/10/9 14:26
# @Author  : mmceil
# @File    : SourceToMD.py
# @Software: PyCharm
import os
import sys


def get_parent(path):
    data = path.split("\\")
    if len(data)<1:
        data = path.split("/")
    return data[len(data)-1]


def run(path,param,code):
    with open(path+"\\SourceToMDResult.md", "w",encoding="utf-8") as out:
        for file_path,dirs,files in os.walk(path):
            file_parent = get_parent(file_path)
            out.write("## ")
            out.write(file_parent)
            out.write("\n")
            print(file_parent)
            for file_name in files:
                file_all_name = os.path.join(file_path,file_name)
                if file_all_name.split(".")[-1] == param:
                    out.write(file_name)
                    out.write("\n")
                    out.write("```"+param)
                    out.write("\n")

                    with open(file_all_name, "r",encoding=code) as f:  # 打开文件
                        data = f.read()  # 读取文件
                        out.write(data)
                        out.write("\n")
                    out.write("```")
                    out.write("\n")
            pass



def get_sys():
    path = sys.argv[1]
    param = sys.argv[2]
    if len(sys.argv) == 3:
        code = sys.argv[3]
    else:
        code = "utf-8"
    return path,param,code

if __name__ == "__main__":
    # 获取命令行参数
    # path,param,code = get_sys()
    # path = "E:\\Scala（新版）\\3.代码\\scala_tutorial\\src\\main\\scala"
    path = "E:\\Scala（新版）\\3.代码\\scala_tutorial\\src\\main\\java"
    code = "utf-8"
    param = "java"
    run(path,param,code)



